package com.sto.stoapp.manager;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

import com.sto.stoapp.activity.R;

import java.util.HashMap;
import java.util.Map;

/**
 * 音频管理类
 *
 * @author Administrator
 *
 */
public class SoundManager {
	private MediaPlayer player;

	private Context context;

	// private boolean isSuccess = false, isFailure = false;

	private static SoundManager soundManager;

	// private AudioManager am;

	/**
	 * 存放声音ID的Map集合
	 */
	private Map<Integer, Integer> spMap = new HashMap<Integer, Integer>();

	/**
	 * 声音池
	 */
	private SoundPool sp;

	public static final int SCAN_ABNORMAL = 1;
	public static final int SCAN_NORMAL = 2;
	public static final int PHOTO = 3;
	public static final int WEIGHT_SOUND = 4;
	public static final int FILTER_SOUND = 5;
	public static final int WAITE_PAY=6;

	public SoundManager(Context context) {
		this.context = context;
		// am = (AudioManager) context
		// .getSystemService(Context.AUDIO_SERVICE);
		// int maxVol = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		// am.setStreamVolume(AudioManager.STREAM_MUSIC, maxVol,// 备注2
		// AudioManager.FLAG_PLAY_SOUND);

		initSoundPool();
	}

	public static SoundManager getInstance(Context context) {
		if (soundManager == null) {
			soundManager = new SoundManager(context);
		}
		return soundManager;
	}

	/**
	 * 初始化声音池
	 */
	public void initSoundPool() {
		try {
			sp = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
			spMap.put(SCAN_NORMAL, sp.load(context, R.raw.scan_new0, 1));
			spMap.put(SCAN_ABNORMAL, sp.load(context, R.raw.scan_abnormal, 1));
			spMap.put(PHOTO, sp.load(context, R.raw.photo, 1));
			spMap.put(WEIGHT_SOUND, sp.load(context, R.raw.beep, 1));
			spMap.put(FILTER_SOUND,sp.load(context,R.raw.filter,1));
			spMap.put(WAITE_PAY,sp.load(context,R.raw.waite_pay,1));

		} catch (Exception e) {
			e.printStackTrace();
		}
		// spMap.put(TOUCH_KEY, sp.load(context, R.raw.key, 1));
	}

	/**
	 * 播放扫描成功声音
	 */
	public void playSuccessSound() {
		// am.setStreamVolume(AudioManager.STREAM_MUSIC,
		// am.getStreamVolume(am.getRingerMode()), 0);
		playSound(SCAN_NORMAL);
		/*
		 * if (!isSuccess) { isSuccess = true; isFailure = false; //
		 * soundManager.releases(); // MediaPlayer的初始化 player =
		 * MediaPlayer.create(context, R.raw.scan_normal_1);
		 * player.setLooping(false);// 设置循环播放 } player.start();
		 */
	}

	/**
	 * 播放扫描失败声音
	 */
	public void playFailureSound() {
		// am.setStreamVolume(AudioManager.STREAM_MUSIC,
		// am.getStreamVolume(am.getRingerMode()), 0);
		playSound(SCAN_ABNORMAL);
		/*
		 * if (!isFailure) { isFailure = true; isSuccess = false; //
		 * soundManager.releases(); // MediaPlayer的初始化 //am =
		 * (AudioManager)context.getSystemService(context.AUDIO_SERVICE); player
		 * = MediaPlayer.create(context, R.raw.scan_abnormal);
		 * //player.setVolume(am.getStreamVolume(AudioManager.STREAM_SYSTEM),
		 * am.getStreamVolume(AudioManager.STREAM_SYSTEM));
		 * player.setLooping(false);// 设置循环播放 } player.start();
		 */
	}

	public void playWeightSound() {
		playSound(WEIGHT_SOUND);
	}

	/**
	 * 拍照声音
	 */
	public void playPhotoSound() {
		playSound(PHOTO); // 播放声音
	}

	/**
	 * 播放按键和触屏声音
	 */
	public void playKeyAndTouchSound() {
		// am.setStreamVolume(AudioManager.STREAM_MUSIC,
		// am.getStreamVolume(am.getRingerMode()), 0);
		if (player != null) {
			player.release();

			player = null;
		}
		player = MediaPlayer.create(context, R.raw.key);
		player.setLooping(false);// 设置循环播放
		player.start();
	}

	/**
	 * 播放声音,调用该方法前,需要先调用initSoundPool()方法,否则会异常
	 *
	 * @param soundKey
	 *            ：保存声音ID的key,这里为SCAN_ABNORMAL(扫描失败)、SCAN_NORMAL(扫描成功)
	 */
	private void playSound(int soundKey) {
		AudioManager am = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		float maxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		float currentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);
		float ratioVolumn = currentVolumn / maxVolumn;
		// 第一个参数:待播放的音频ID,二三参数：左右声道,第四个参数：优先级,第五个：循环次数,第六个：回放速度,值在0.5-2.0之间,1为正常速度
		sp.play(spMap.get(soundKey), ratioVolumn, ratioVolumn, 1, 0, 1f);
	}

	/**
	 * 释放MediaPlay
	 */
	public void releases() {
		if (sp != null) {
			sp.release();
		}
		/*
		 * if (player != null) { player.release(); }
		 */
	}

	/**
	 * 播放过滤声音
	 */
	public void playFilterSound() {

		playSound(FILTER_SOUND);
	}

	/**
	 * 代收货款提示音
	 */
	public void playWaitePaySound(){
		playSound(WAITE_PAY);
	}
}
